package rs.networx.servlets;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import rs.networx.dao.PersonDao;
import rs.networx.entity.Person;
import rs.networx.entity.Post;

public class GoToSelectedPersonProfileServlet extends HttpServlet {

	private static final long serialVersionUID = -2703233907826919687L;

	@EJB
	private PersonDao personDao;
	
	private String selectedPersonID;
	private Integer intSelectedPersonID;
	private Person selectedPerson;
	
	private String username;
	private String password;
	private Integer intLoggedUserID;
	private Person loggedUser;
	
	private List<Post> selectedPersonForMembersBrowsingPosts;
	
	private boolean areFriends;
	
	private String cleanedUpDateOfBirth;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

		System.out.println("LOADED GoToSelectedPersonProfileServlet ");

		response.setContentType("text/html; charset=UTF-8");
		request.setCharacterEncoding(response.getCharacterEncoding());
		
		HttpSession session = request.getSession();
		loggedUser = (Person)session.getAttribute("logovaniUser");
		
		selectedPersonID = request.getParameter("personID");
		
		try {
			System.out.println("selectedPersonID: " + selectedPersonID);
			
			intSelectedPersonID = Integer.parseInt(selectedPersonID);
			System.out.println("intSelectedPersonID: " + intSelectedPersonID);
			
			System.out.println(selectedPersonID + "  " + intSelectedPersonID);
			
			username = loggedUser.getEmail();
			password = loggedUser.getPassword();
			intLoggedUserID = loggedUser.getId();
			
			System.out.println(username + "  " + password + "  " + intLoggedUserID);
		} catch (Exception e) {
			//e.printStackTrace();
		}
		
		if(intSelectedPersonID != null){
			// nadjemo selektovanu osobu za members browsing...
			selectedPerson = personDao.findById(intSelectedPersonID);
			System.out.println("selectedPerson: " + selectedPerson.getEmail());
			
			if(loggedUser != null){
				if(selectedPerson != null){
					// vidimo da li su logovani user i selected person za members browsing prijatelji, ako jesu tada logovani user moze da komentarise...
					// na zidu selected person za members browsing, a ako nisu onda ne moze...
					areFriends = personDao.areFriends(intLoggedUserID, intSelectedPersonID);
					System.out.println("areFriends: " + areFriends);
					
					// dobavljamo iz baze postove za selektovanu osobu za members browsing...
					selectedPersonForMembersBrowsingPosts = personDao.getFirstTenOwnedPosts(selectedPerson.getId());
					
					cleanedUpDateOfBirth = cleanUpDateOfBirth(selectedPerson.getDob());
					System.out.println(cleanedUpDateOfBirth);
					
					/*// sada kacimo podatke na sesiju i zahtev...
					session.setAttribute("selectedPersonForMembersBrowsing", selectedPerson);
					session.setAttribute("selectedPersonForMembersBrowsingPosts", selectedPersonForMembersBrowsingPosts);
					session.setAttribute("areFriends", areFriends);*/
					
					// sada kacimo podatke na zahtev...
					session.setAttribute("selectedPersonForMembersBrowsing", selectedPerson);
					session.setAttribute("selectedPersonForMembersBrowsingPosts", selectedPersonForMembersBrowsingPosts);
					session.setAttribute("areFriends", areFriends);
					session.setAttribute("cleanedUpDateOfBirthForMembersBrowsing", cleanedUpDateOfBirth);
					
					RequestDispatcher disp = request.getRequestDispatcher("SelectedPersonPostsPage.jsp");
					disp.forward(request, response);
					
				}else if(selectedPerson == null){
					request.getRequestDispatcher("WelcomePage.jsp").forward(request, response);
					return;
				}
			}else if(loggedUser == null){
				request.getRequestDispatcher("Welcome.jsp").forward(request, response);
				return;
			}
		}else if(intSelectedPersonID == null){
			request.getRequestDispatcher("WelcomePage.jsp").forward(request, response);
			return;
		}	
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		doGet(request, response);
	}
		private String cleanUpDateOfBirth(Date dateOfBirth){
		String result = null;
		
		if(selectedPerson != null){
			System.out.println(dateOfBirth);
			
			String dateOfBirthDB = dateOfBirth.toString();
			
			int i = dateOfBirthDB.indexOf(" ");
			System.out.println("i: " + i);
			
			String temp = dateOfBirthDB.substring(0, i);
			System.out.println("ddd: " + temp);
			
			String[] array = temp.split("-");
			
			System.out.println(array[0]);
			System.out.println(array[1]);
			System.out.println(array[2]);
			System.out.println(array[2] + "/" + array[1] + "/" + array[0]);
			
			result = array[2] + "/" + array[1] + "/" + array[0];
		}
		return result;
	}
}
